﻿using System;
using System.Collections.Generic;
using System.IO;

namespace ExcelConfig.Config
{
    public class AppConfigStreamFile : IExcelConfig, IDisposable
    {
        /// <summary>
        /// 主配置文件
        /// </summary>
        public ExcelConfigStreamFile ExcelConfig { get; internal set; }
        internal string sheet { get; set; }
        public AppConfigStreamFile(Stream stream)
        {
            ExcelConfig = new ExcelConfigStreamFile(stream);
            this.sheet = ExcelConfigSetting.SHEET_Global;
        }

        public void GotoSheet(string sheetname)
        {
            if (ExcelConfig.ContainSheets(sheetname)) this.sheet = sheetname;
        }
        /// <summary>
        /// 读取全局配置值
        /// </summary>
        public T ReadGobalValue<T>(string key)
        {
            return ExcelConfig.ReadValue<T>(ExcelConfigSetting.SHEET_Global, key);
        }
        public T ReadValue<T>(string key)
        {
            return ExcelConfig.ReadValue<T>(this.sheet, key);
        }
        public void WriteValue(string key, string value)
        {
            ExcelConfig.WriteValue(this.sheet, key, value);
        }
        public void WriteGlobalValue(string key, string value)
        {
            ExcelConfig.WriteGlobal(key, value);
        }
        /// <summary>
        /// 读取文本
        /// </summary>
        public string Text(string key)
        {
            return ExcelConfig.ReadText(this.sheet, key);
        }
        public decimal Number(string key)
        {
            return ReadValue<decimal>(key);
        }
        /// <summary>
        /// 写入全局文本
        /// </summary>
        public void WriteText(string key, string value)
        {
            WriteValue(key, value);
        }
        /// <summary>
        /// 读取全局数组
        /// </summary>
        public string[] Array(string key)
        {
            var arr = new Queue<string>();
            foreach (var text in LoopArray(key)) arr.Enqueue(text);
            return arr.ToArray();
        }
        public IEnumerable<string> LoopArray(string key)
        {
            var arr = Text(key).Split(ExcelConfigSetting.ArraySplitChar);
            for (var i = 0; i < arr.Length; i++) if (!string.IsNullOrEmpty(arr[i])) yield return arr[i];
        }
        /// <summary>
        /// 读取全局开关值
        /// </summary>
        public bool YesOrNo(string key)
        {
            return Text(key) == ExcelConfigSetting.YES;
        }
        /// <summary>
        /// 写入全局开关值
        /// </summary>
        public void WriteYesOrNo(string key, bool value)
        {
            WriteValue(key, value ? ExcelConfigSetting.YES : ExcelConfigSetting.NO);
        }
        public void WriteYes(string key)
        {
            WriteYesOrNo(key, true);
        }
        public void WriteNo(string key)
        {
            WriteYesOrNo(key, false);
        }
        public void ToStream(Stream mem)
        {
            ExcelConfig.ToStream(mem);
        }
        public void Dispose()
        {
            if (ExcelConfig != null)
            {
                ExcelConfig.Dispose();
                ExcelConfig = null;
            }
        }
    }
}
